package com.ddf.controller;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ddf.entity.Address;
import com.ddf.entity.User;

@Controller
@RequestMapping("/requestParamDemo")
public class RequestParamDemo {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	private static final String SUCCESS = "success";
	
	
	/**
	 * required 默认true,是否必须，如果必须不传此参数，则无法映射请求。
	 * defaultValue 默认值
	 * @param userName
	 * @param age
	 * @return
	 */
	@RequestMapping("/testRequestParams")
	public String requestParams(@RequestParam String userName, @RequestParam(
			required = false, defaultValue = "0")Integer age) {
		
		log.info("requestParams is invork ! userName:" + userName +
				"age:" + age);
		return SUCCESS;
	}
	
	/**
	 * @RequestHeader可以将请求头中的参数映射出来
	 * @param ct
	 * @return
	 */
	@RequestMapping("/testRequestHeader")
	public String requestHeader(@RequestHeader(value = "content-type") String ct) {
		log.info("requestHeader  content-type:" + ct);
		return SUCCESS;
	}
	
	/**
	 * @CookieValue用來映射請求中的cookie參數
	 * @param sessionId
	 * @return
	 */
	@RequestMapping("/testCookieValue")
	public String cookieValue(@CookieValue("JSESSIONID") String sessionId) {
		log.info("cookieValue  JSESSIONID:" + sessionId);
		return SUCCESS;
	}
	
	/**
	 * 參數為POJO，參數名與對象保持一致，支持級聯對象。
	 * @param user
	 * @return
	 */
	@RequestMapping("/testPojoParams")
	public String pojoParams(User user) {
		log.info("userName:" + user.getUserName() + ", sex:" + user.getSex()
						+ ", email:" + user.getEmail());
		Address address = user.getAddress();
		if(address != null) {
			log.info("province:" + address.getProvince() + ",city:" + address.getCity());
		}
		return SUCCESS;
	}
	
	/**
	 * 直接在参数中可以传入以下常用serlvet 对象
	 * HttpServletRequest
	 * HttpServletResponse
	 * Writer
	 * Reader
	 * OutPutStream
	 * InputStream
	 */
	@RequestMapping("/testServletAPI")
	public void testServletAPI(HttpServletRequest request, HttpServletResponse response,
			Writer writer) {
		String userName = request.getParameter("userName");
		String age = request.getParameter("age");
		try {
			StringBuilder sbl = new StringBuilder();
			sbl.append("<Xml>");
			sbl.append("<Person>");
			sbl.append("<userName>" + userName +  "</userName>");
			sbl.append("<age>" + age +  "</age>");
			sbl.append("</Person>");
			sbl.append("</Xml>");
			response.setHeader("content-type", "application/xml");
			log.info(sbl.toString());
			writer.write(sbl.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
